package com.xhu.controller;

import com.xhu.vo.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import java.util.List;
import java.util.Map;


/**
 * Created by yuan on 2019/3/13.
 */
@Controller
@RequestMapping("/user") // url和类绑定
@SessionAttributes({"logUser"}) //自动将ModelMap中名为logUser的对象同步到HttpSession中
public class UserController {

    static Logger logger = Logger.getLogger(UserController.class);

    /**
     * @return
     */
    @RequestMapping("/login")  // /user/login
    public String login(User user, ModelMap modelMap){
        //modelMap为请求作用域
        logger.debug("登录：" + user);
        StringBuffer sql= new StringBuffer("select password from user where name='"+user.getUserName()+"'");
        Client.ClientDo(sql.toString());
        Map<Integer, List> password = Client.selectResultMap;
        String s = password.get(1).toString();
        String substring = s.substring(1, s.length() - 1);
        if (user.getUserName() != null && user.getPassword().equals(substring)) {
            modelMap.addAttribute("logUser",user);
            return "redirect:/index.jsp";
        }
        // 会被InternalResourceViewResolver解析成一个页面：(prefix + string + suffix) = /views/success.jsp
        // springmvc默认页面导航为请求转发
        return "redirect:/login.jsp";
    }


}
